package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.myshop.ShopAddGoodsActivity;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.OrderListPresenter;
import cn.xfdzx.android.apps.shop.presenter.ShopInGoodListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;

import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**
 * @author gzk
 * @date 2020/5/18 0018
 * description：
 */
public class ShopInGoodListAdapter extends RecyclerView.Adapter {


    private List<ShopInGoodListBeen.DataBean> mList;
    private ItemClickListener itemClickListener;


    private Activity mContext;
    private final LayoutInflater mLayoutInflater;
    private ShopInGoodListPresenter mPresenter;
    private String styleType;


    public ShopInGoodListAdapter(Activity mContext, List<ShopInGoodListBeen.DataBean> list, ShopInGoodListPresenter mPresenter,String styleType) {
        this.mContext = mContext;
        this.mList = list;
        this.mPresenter = mPresenter;
        this.styleType = styleType;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ShopInGoodListAdapter.OrderViewHolder0(mContext, mLayoutInflater.inflate(R.layout.item_shopin_good_list, null));


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        ShopInGoodListAdapter.OrderViewHolder0 orderViewHolder0 = (ShopInGoodListAdapter.OrderViewHolder0) holder;
        orderViewHolder0.setData(position);


    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class OrderViewHolder0 extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_good_image)
        ImageView ivGoodImage;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.tv_good_price)
        TextView tvGoodPrice;
       @BindView(R.id.tv_good_old_price)
        TextView tvGoodOldPrice;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_inventory)
        TextView tvInventory;
        @BindView(R.id.tv_kuai)
        TextView tvKuai;
        @BindView(R.id.tv_editor)
        TextView tvEditor;
        @BindView(R.id.tv_good_top)
        TextView tvGoodTop;


        private Activity mContext;

        public OrderViewHolder0(Activity mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;

        }

        public void setData(int position) {
            if (mList.get(position).getState() == 0) {
                tvGoodTop.setText("商品上架");
                tvGoodTop.setTextColor(mContext.getResources().getColor(R.color.color_FFFFFF));
                tvGoodTop.setBackgroundResource(R.drawable.d02c557_bg_r3);
            } else {
                tvGoodTop.setText("商品下架");
                tvGoodTop.setTextColor(mContext.getResources().getColor(R.color.color_333333));
                tvGoodTop.setBackgroundResource(R.drawable.d333333_outside_05_r2);

            }

            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
            Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivGoodImage);

            tvGoodName.setText(mList.get(position).getTitle());

            tvNum.setText("销量：" + mList.get(position).getSale_num());
            tvInventory.setText("库存：" + mList.get(position).getStock());
            if (Double.parseDouble(mList.get(position).getDiscountprice())==0){
                tvGoodPrice.setText("¥" + mList.get(position).getMarketprice());
                tvGoodOldPrice.setVisibility(View.GONE);

            }else {
                tvGoodOldPrice.setVisibility(View.VISIBLE);
                tvGoodOldPrice.setText("¥" + mList.get(position).getMarketprice());
                tvGoodPrice.setText("¥" + mList.get(position).getDiscountprice());
            }
            tvGoodOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            tvKuai.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        itemClickListener.onItemClick(position);
                    }
                }
            });
            tvEditor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, ShopAddGoodsActivity.class);
                        intent.putExtra("good_id", mList.get(position).getId() + "");
                        intent.putExtra("type", styleType);
                        mContext.startActivityForResult(intent, START_CODE);
                    }
                }
            });
            tvGoodTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        if (mList.get(position).getState() == 0) {
                            mPresenter.goodsUpState(mList.get(position).getId() + "", "1", position);
                        } else {
                            mPresenter.goodsUpState(mList.get(position).getId() + "", "0", position);
                        }
                    }
                }
            });

        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
